import {navList} from '../common/js/base'
const keyBy = require('lodash/keyBy')
// 将navlist 变为key => value all: {title:'全部',type:'all'}
const listMap = keyBy(navList, 'type')
export function getTopicType(val = 'share') {
  return listMap[val].title
}

function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

export function getTimeInfo(str) {
  if (!str) return ''
  const s = 60
  const h = 24
  const M = 31
  const y = 12
  const ss = 1000
  const date = new Date(str)
  const time = new Date().getTime() - date.getTime() // 当前时间 - 传入时间 = 相差的时间 （单位：毫秒）
  if (time < 0) {
    return ''
  } else if (time / ss < s) {
    return '刚刚'
  } else if (time / (ss * s) < s) {
    return `${parseInt(time / (ss * s))} 分钟前`
  } else if (time / (ss * s * s) < h) {
    return `${parseInt(time / (ss * s * s))} 小时前`
  } else if (time / (ss * s * s * h) < M) {
    return `${parseInt(time / (ss * s * s * h))} 天前`
  } else if (time / (ss * s * s * h * M) < y) {
    return `${parseInt(time / (ss * s * s * h * M))} 月前`
  } else {
    return `${parseInt(time / (ss * s * s * h * M * y))} 年前`
  }
}
